package com.bzs.batch.reconcile.batch.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bzs.batch.reconcile.entity.HostFile;
import com.bzs.batch.reconcile.entity.ReconcileBatch;
import com.bzs.batch.reconcile.entity.SysTransactionflow;
import com.bzs.batch.reconcile.mapper.ReconcileBatchMapper;
import com.bzs.batch.reconcile.mapper.SysTransactionflowMapper;
import lombok.AllArgsConstructor;
import org.springframework.batch.core.ItemReadListener;
import org.springframework.batch.core.ItemWriteListener;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname listener
 * @Author baozs
 * @Date 2021/12/29 13:38
 * @Version 1.0
 * @Description
 **/

@Configuration
@AllArgsConstructor
public class Listener {
    private final ReconcileBatchMapper reconcileBatchMapper;
    private final SysTransactionflowMapper sysTransactionflowMapper;

    @Bean
    @StepScope
    public ItemWriteListener<SysTransactionflow> getDataToFileListener(@Value("#{jobParameters[date]}") String date) {
        return new ItemWriteListener<SysTransactionflow>() {
            @Override
            //写文件之前把总金额和总笔数计算出来，存到数据库
            public void beforeWrite(List<? extends SysTransactionflow> items) {
                System.out.println("写文件之前调用");
               /* ReconcileBatch reconcileBatch = new ReconcileBatch();
                reconcileBatch.setBatchNo(date);
                //总笔数
                reconcileBatch.setBatchCount(String.valueOf(items.size()));
                //总金额
                int batch = 0;
                for (SysTransactionflow item : items) {
                    int anInt = Integer.parseInt(item.getTransAmount());
                    batch += anInt;
                }
                reconcileBatch.setHatchAmount(String.valueOf(batch));
                int insert = reconcileBatchMapper.insert(reconcileBatch);
                if (insert > 0) {
                    System.out.println("房管局系统文件总笔数：" + items.size() + "和总金额：" + batch + "保存成功");
                }*/
            }

            @Override
            //写完文件更新状态
            public void afterWrite(List<? extends SysTransactionflow> items) {
                UpdateWrapper<SysTransactionflow> updateWrapper = new UpdateWrapper<>();
                ArrayList<String> sysSerialnos = new ArrayList<>();
                for (SysTransactionflow item : items) {
                    String sysSerialno = item.getSysSerialno();
                    sysSerialnos.add(sysSerialno);
                }
                updateWrapper.in("sys_serialno", sysSerialnos);
                updateWrapper.set("push_status", "01");
                int update = sysTransactionflowMapper.update(new SysTransactionflow(), updateWrapper);
                System.out.println("向房管局文件推送成功笔数：" + update);
            }

            @Override
            public void onWriteError(Exception exception, List<? extends SysTransactionflow> items) {
                System.out.println("向房管局写文件报错：" + exception.getMessage());
            }
        };
    }

    @Bean
    @StepScope
    public ItemWriteListener<HostFile> getFileToDataListener(@Value("#{jobParameters[date]}") String date) {
        return new ItemWriteListener<HostFile>() {
            @Override
            //校验文件的总笔数和总金额
            public void beforeWrite(List<? extends HostFile> items) {
                QueryWrapper<ReconcileBatch> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("batch_no", date);
                ReconcileBatch reconcileBatch = reconcileBatchMapper.selectOne(queryWrapper);
                String batchCount = reconcileBatch.getBatchCount();
                int anInt = Integer.parseInt(batchCount);
                boolean is_rCount = items.size() == anInt;
                int batchAmount = 0;
                if (!is_rCount) {
                    System.out.println("Host系统文件总笔数有误：批次号：" + date);
                }
                for (HostFile item : items) {
                    int TransAmount = Integer.parseInt(item.getTransAmount());
                    batchAmount += TransAmount;
                }
                String hatchAmount = reconcileBatch.getHatchAmount();
                boolean is_rAmount = Integer.parseInt(hatchAmount) == batchAmount;
                if (!is_rAmount) {
                    System.out.println("Host系统文件总金额有误");
                }
            }

            @Override
            public void afterWrite(List<? extends HostFile> items) {

            }

            @Override
            public void onWriteError(Exception exception, List<? extends HostFile> items) {

            }
        };
    }
}
